package com.sys.common.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LGY
 * Create by 2024/5/23 17:20
 */
@Slf4j
@Getter
@Configuration
@ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true")
public class XxlJobConfig {

    @Value("${fast.xxl.job.adminAddress}")
    private String adminAddresses;

    @Value("${fast.xxl.job.accessToken}")
    private String accessToken;

    @Value("${fast.xxl.job.executor.applicationName}")
    private String appname;

    @Value("${fast.xxl.job.executor.address:}")
    private String address;

    @Value("${fast.xxl.job.executor.ip}")
    private String ip;

    @Value("${fast.xxl.job.executor.port}")
    private int port;

    @Value("${fast.xxl.job.executor.logPath}")
    private String logPath;

    @Value("${fast.xxl.job.executor..logretentionDays:30}")
    private int logRetentionDays;


    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setAddress(address);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }
}
